/*
 * 版权信息：北京汉方三陆零科技有限公司</br>
 * Copyright ©2010-2014. All rights reserved.  京ICP备15000045号
 */
package com.lin.fund;

import java.io.File;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSONObject;

/****************************************************************************
 * com.hf.utils HttpUtils.java Created on 2015年1月28日
 * @Author: linfenliang
 * @Description:
 * @Version: 1.0
 ***************************************************************************/
public class HttpUtils {
	private static final Logger logger = LogManager.getLogger(HttpUtils.class);
	/**
	 * 
	 * 
	 * @param url
	 * @return
	 * String
	 * @Date 2015年2月5日
	 * @Version 1.0
	 * @Author linfenliang
	 */
	private static final String httpGet(String url){
		return httpGet(url, "UTF-8");
	}
	private static final String httpGet(String url,String charset){
		String result = null;
		HttpGet httpGet = new HttpGet(url);
		try (CloseableHttpClient httpclient = HttpClients.createDefault();
				CloseableHttpResponse resp = httpclient.execute(httpGet);){
			HttpEntity entity = resp.getEntity();
			result = EntityUtils.toString(entity,charset);
			EntityUtils.consume(entity);
		} catch (ClientProtocolException e) {
			logger.warn(e.getMessage(), e);
		} catch (IOException e) {
			logger.warn(e.getMessage(), e);
		}
		return result;
	}
	public static final byte[] getByteArray(String url){
		byte[] result = null;
		HttpGet httpGet = new HttpGet(url);
		try (CloseableHttpClient httpclient = HttpClients.createDefault();
				CloseableHttpResponse resp = httpclient.execute(httpGet);) {
			HttpEntity entity = resp.getEntity();
			result = EntityUtils.toByteArray(entity);
			EntityUtils.consume(entity);
		} catch (ClientProtocolException e) {
			logger.warn(e.getMessage(), e);
		} catch (IOException e) {
			logger.warn(e.getMessage(), e);
		}
		return result;
	}
	/**
	 * POST方式发送JSON数据
	 * 
	 * @param url
	 * @param params
	 * @return
	 * String
	 * @Date 2015年4月17日
	 * @Version 1.0
	 * @Author linfenliang
	 */
	public static final String postJSON(String url,JSONObject params){
		String result = null;
		HttpPost httpPost = new HttpPost(url);
		try (CloseableHttpClient httpclient = HttpClients.createDefault();
				CloseableHttpResponse resp = httpclient.execute(httpPost);){
			StringEntity p =new StringEntity(params.toJSONString(),"UTF-8");
			httpPost.addHeader("content-type", "application/json");  
			httpPost.setEntity(p);  
			HttpEntity entity = resp.getEntity();
			result = EntityUtils.toString(entity);
			EntityUtils.consume(entity);
		} catch (ClientProtocolException e) {
			logger.warn(e.getMessage(), e);
		} catch (IOException e) {
			logger.warn(e.getMessage(), e);
		}
		return result;
	}
	/**
	 * POST方式发送XML数据
	 * 
	 * @param url
	 * @param params
	 * @return
	 * String
	 * @Date 2015年4月17日
	 * @Version 1.0
	 * @Author linfenliang
	 */
	public static final String postXml(String url,String params){
		String result = null;
		HttpPost httpPost = new HttpPost(url);
		try (CloseableHttpClient httpclient = HttpClients.createDefault();
				CloseableHttpResponse resp = httpclient.execute(httpPost);){
			StringEntity p =new StringEntity(params,"UTF-8");
			httpPost.setHeader("content-type", "text/xml");
			httpPost.setEntity(p);  
			HttpEntity entity = resp.getEntity();
			result = EntityUtils.toString(entity,"UTF-8");
			EntityUtils.consume(entity);
		} catch (ClientProtocolException e) {
			logger.warn(e.getMessage(), e);
		} catch (IOException e) {
			logger.warn(e.getMessage(), e);
		}
		return result;
	}
	public static final String getContent(String url){
		return httpGet(url);
	}
	public static final String getContent(String url,String charset){
		return httpGet(url,charset);
	}
	/**
	 * 
	 * 
	 * @param url
	 * @return
	 * String
	 * @Date 2015年2月6日
	 * @Version 1.0
	 * @Author linfenliang
	 */
	public static final String getContent(String url,int maxRetryTimes){
		String content = httpGet(url);
		int sleepTime = 1;
		while(content == null && sleepTime <=maxRetryTimes){
			logger.warn("数据获取失败第{}次重试！",sleepTime);
			try {Thread.sleep(1000*sleepTime);} catch (InterruptedException e) {}
			content = HttpUtils.httpGet(url);
			sleepTime++;
		}
		return content;
	}
	public static final String getContent(String url,String startStr,String endStr){
		String content = HttpUtils.httpGet(url);
		return StringUtils.substringBetween(content, startStr, endStr);
	}
	public static final String getContent(String url,String charset, String startStr,String endStr){
		String content = HttpUtils.httpGet(url,charset);
		return StringUtils.substringBetween(content, startStr, endStr);
	}
	public static final String getContent(String url,String startStr,String endStr, int maxRetryTimes){
		String content = HttpUtils.httpGet(url);
		int sleepTime = 1;
		while(content == null && sleepTime <=maxRetryTimes){
			logger.warn("数据获取失败第{}次重试！",sleepTime);
			try {Thread.sleep(1000*sleepTime);} catch (InterruptedException e) {}
			content = HttpUtils.httpGet(url);
			sleepTime++;
		}
		return StringUtils.substringBetween(content, startStr, endStr);
	}
	/**
	 * 上传文件到服务器
	 * 
	 * @param url
	 * @param file
	 * @return
	 * String
	 * @Date 2015年5月22日
	 * @Version 1.0
	 * @Author linfenliang
	 */
	public static final String uploadFile(String url,File file){
		HttpPost httpPost = new HttpPost(url);
		FileBody bin = new FileBody(file);
		HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("media", bin).build();
		httpPost.setEntity(reqEntity);
		HttpEntity entity = null;
		String result = null;
		try (CloseableHttpClient httpclient = HttpClients.createDefault();
				CloseableHttpResponse resp = httpclient.execute(httpPost);) {
			entity = resp.getEntity();
			result = EntityUtils.toString(entity, "UTF-8");
		} catch (ClientProtocolException e) {
			logger.warn("上传多媒体数据异常",e);
		} catch (ParseException e) {
			logger.warn("上传多媒体数据异常",e);
		} catch (IOException e) {
			logger.warn("上传多媒体数据异常",e);
		}
		return result;
	}
	
}
